{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Learn the Python Math Module.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyOwgJhxR8T7UydRYxOPlUbq",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Learn_the_Python_Math_Module.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iOM74FRHcq3g",
        "colab_type": "text"
      },
      "source": [
        "# Learn the Python Math Module"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "g-hJaHmZc2UC",
        "colab_type": "text"
      },
      "source": [
        "In this tutorial, I will show you how to use to the builtin Python `math` module."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mf1bX3MQc76M",
        "colab_type": "text"
      },
      "source": [
        "![alt text](https://miro.medium.com/max/743/1*EfK0rFMPhh57Q70auf8T_w.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rPl_xuiadFJ1",
        "colab_type": "text"
      },
      "source": [
        "# What is the math module?\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FCvwxdR2dIvR",
        "colab_type": "text"
      },
      "source": [
        "It is the C standard defined math module. The math module provides access to mathematical functions.\n",
        "\n",
        "I know that I have used some jargon in the above statement. Let me clear the clutter. The important terms in the above statement are ***module, C standard, mathematical functions***."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zJe5CnI1dOyY",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "*   **Module**: A file containing a set of **statements** and **definitions** is called a module. For example, the file `program.py` is called a module and the module name would be `program`. You will know how to use a module in the later tutorial.\n",
        "\n",
        "*   **C standard**: We also call it `libc` (which means library C), which is the standard library of the C programming language. The C standard library provides several string handling functions, data types, mathematical computations, process control, and many more.\n",
        "\n",
        "*   **Mathematical functions**: These are a group of functions provided by the standard C library. They include `abs`, `labs`, `div`, `max`, `min`, and many more. To read more about the mathematical functions, there’s nothing better than [Wikipedia](https://en.wikipedia.org/wiki/C_mathematical_functions).\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZvvM2dNSd1wA",
        "colab_type": "text"
      },
      "source": [
        "# Why do we need to use the math module?\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SrUQXTeld5Ta",
        "colab_type": "text"
      },
      "source": [
        "Well, the answer is simple: Whenever we need to perform mathematical operations then the math module comes in handy. Let me give you a simple example. It’s quiz time!\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ch7vaPiNd8Xc",
        "colab_type": "text"
      },
      "source": [
        "## Calculate the area of a regular python with 4 sides and length of a side is 20\n",
        "\n",
        "Let give me you some parameters and the formulae of the area. Now just calculate the area without using the math module in python. Pretty challenging right?\n",
        "\n",
        "**The formulae to find the area of a regular polygon is:**\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1174/1*PbzNnTSCatkptevYEhja1g.png)\n",
        "\n",
        "\n",
        "**Now the parameters are:**\n",
        "\n",
        "*  n = number of sides\n",
        "*  l = length of a side\n",
        "\n",
        "Now you have things clarified, give me the answer (the program to solve it without using the math module) in about 5–7 minutes. You can post the answer in the comment section below.\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/2115/1*7DBF85m70NNuujpg7uEBMA.png)\n",
        "\n",
        "\n",
        "## Solution\n",
        "\n",
        "I know most of you guys might end up calculating the values using a calculator and then put that in the formulae and then display the result programmatically. It will get the correct solution, but it is definitely not recommended.\n",
        "\n",
        "\n",
        "**Let me give you the program first (using math module) and then I will explain it line by line**\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pp_A8CNEfNU_",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "f351de5f-0d2b-4de8-9a96-c4f885aa72c1"
      },
      "source": [
        "import math\n",
        "area = (4 * math.pow(20, 2))/(4 * math.tan(math.pi/4))\n",
        "print(area)"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "400.00000000000006\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i-wzJoIxfR2D",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "*   The first step is to import the math module. This can be done as `import math`. This is what gives you access to using its mathematical methods.\n",
        "\n",
        "*   The second step is just substituting the values to the formulae. Now here is the significance of the math module. I have used three important methods of the math module and those are `math.pow()`, `math.tan()`, `math.pi`.\n",
        "\n",
        "The `math.pow()` → It is a built-in power function that is used to calculate the exponent of the given number used in the math module.\n",
        "\n",
        "\n",
        "```\n",
        "pow(...)     \n",
        "    pow(x, y)          \n",
        "    Return x**y (x to the power of y).\n",
        "```\n",
        "\n",
        "## Calculating 150 to the power 4\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NtQUWBn0gdLE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5af667dd-0ae7-4860-adc2-5d38c7ea0bb2"
      },
      "source": [
        "import math\n",
        "print(math.pow(150, 4))"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "506250000.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "T_B0RZruggTa",
        "colab_type": "text"
      },
      "source": [
        "The `math.tan()` → It is used to calculate the tangent of a given number in radians.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eKxIPY-TgjwY",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "```\n",
        "tan(...)     \n",
        "    tan(x)          \n",
        "    Return the tangent of x (measured in radians).\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gMTxZmnIgnGm",
        "colab_type": "text"
      },
      "source": [
        "## Calculate the tangent of 60\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "l3XohnCSgq9X",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4f8516e8-02c5-486e-a0cd-ecf0503d08cd"
      },
      "source": [
        "import math\n",
        "print(math.tan(60))"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.320040389379563\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "enyfb98Jgs8t",
        "colab_type": "text"
      },
      "source": [
        "The `math.pi` → As the name suggests is used as the constant value of Pi (3.14159265359). It returns the floating value of pi, rather than memorizing and then writing the value of pi, just use the math module.\n",
        "\n",
        "\n",
        "## Print the value of Pi\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vm4tPQM4gxj7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "498934be-c416-4612-d883-dd9e299a8609"
      },
      "source": [
        "import math\n",
        "print(math.pi)"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3.141592653589793\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YlAJgpM1g0zn",
        "colab_type": "text"
      },
      "source": [
        "Back to the explanation.\n",
        "\n",
        "\n",
        "* The third step is just printing the result, in this case, the area is the final result. 400.00000000000006 is the area of the regular polygon.\n",
        "\n",
        "\n",
        "Now that you guys have mastered the math module, I need you guys to solve the question and let me know the answer below in the comment section. Whoever gets this answer correct will get a **brofist** from me.\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "# Question\n",
        "\n",
        "## Calculate the area of the regular hexagon which has 6 sides.\n",
        "\n",
        "**Hint**: a = 6, also for more details on regular hexagon visit [here](https://byjus.com/maths/area-of-hexagon-formula/)\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "Also, there are exactly 44 functions in the math module. How do I know? Well I literally counted every one of them using the help function\n",
        "\n",
        "`print(help(math))`\n",
        "\n",
        "\n",
        "The above code on executing displays all the mathematical functions in the math module. There would be no reason for me to explain every single function. Just remember one thing, just use the resources only when you need them. There is no need for you need to memorize all the functions. If you are using an [interactive python notebook](https://jupyter.org/), then you can check the syntax of function without even knowing them as shown below:\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1382/1*fYk39dmy0XurELPU-1n_Fw.png)\n",
        "\n",
        "But if you are a geek and you love to read the official documentation of the math module and want to know the syntax of every single function, then there is no better than the [Python math module documentation](https://docs.python.org/3/library/math.html).\n",
        "\n",
        "Anyway, I hope that you guys learned something new today. Learning is the stepping stone of success. So keep learning. Stay tuned for more updates.\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1280/1*0VoACtfamg5rUQyfWD8OhA.jpeg)\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n"
      ]
    }
  ]
}